#include <time.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <utility>
#include <cctype>

// using std::cout;
// using std::endl;


class Dictionary
{
public:
    void _read(std::ifstream& ifs);
    void _write(std::ofstream& ofs);
    bool is_Legal_Word(std::string& s);
private:
    std::map<std::string, std::size_t> _M;
};

void Dictionary::_read(std::ifstream& ifs){
    std::string s;
    std::pair<std::map<std::string, std::size_t>::iterator, bool> p;
    while(ifs >> s){
        if(is_Legal_Word(s) == false){
            continue;
        }
        ++(_M[s]);
    }

}
void Dictionary::_write(std::ofstream& ofs){
    auto it = _M.begin();
    ofs << "--------------------------------------" << std::endl;
    for(auto& m : _M){
        ofs << "|   单词：" << m.first;
        ofs << "\t\t\t\t词频：" << m.second;
        ofs << std::endl;
    }
    ofs << "--------------------------------------" << std::endl;
}

bool Dictionary::is_Legal_Word(std::string& s){
    for(std::string::iterator it = s.begin(); it != s.end(); ++it){
        if(!isalpha(*it)){
            return false;
        }
        if(isupper(*it)){
            (*it) += 32;
        }
    }
    return true;
}



int main(void)
{   
    std::ifstream ifs("The_Holy_Bible.txt");
    std::ofstream ofs("WordFrequency.txt");

    Dictionary D;
    D._read(ifs);
    D._write(ofs);

    ifs.close();
    ofs.close();
    return 0;
}